package com.example.AIstudy.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.example.AIstudy.domain.CuoTiJi;
import com.example.AIstudy.domain.Exercise;
import com.example.AIstudy.domain.FalseReason;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface CuoTiJiDao extends BaseMapper<CuoTiJi> {

    @Select("SELECT * FROM cuotiji ${ew.customSqlSegment}")
    public IPage<CuoTiJi> findCuoTiJi(IPage<CuoTiJi> page,
                                      @Param(Constants.WRAPPER) QueryWrapper<CuoTiJi> wrapper);

    @Select("SELECT count(*) from cuotiji where stuID=#{userID} and (sonProID is not NULL or category in (\"xuanze\",\"zuowen\",\"fanyi\"))")
    public int getCuoTiNumByUserID(Integer userID);

    @Update("update cuotiji set userComment=#{userComment} where id=#{cuotiID}")
    public void updateUserComment(Integer cuotiID,String userComment);

    @Select("select DISTINCT(category) from cuotiji ${ew.customSqlSegment}")
    public List<Object> getCategoriesByTime(@Param(Constants.WRAPPER) QueryWrapper<CuoTiJi> wrapper);

    @Select("select DISTINCT p.* from cuotiji,falsereason as p ${ew.customSqlSegment} and falseReasonID=p.id")
    public List<FalseReason> getFalseReasonByTime(@Param(Constants.WRAPPER) QueryWrapper<CuoTiJi> wrapper);

    @Select("select DISTINCT(field) from cuotiji ${ew.customSqlSegment}")
    public List<Object> getFieldsByTime(@Param(Constants.WRAPPER) QueryWrapper<CuoTiJi> wrapper);

//    @Update("update cuotiji set needRedo=1 where id in (${list})")
    @Update("update cuotiji set needRedo=1 ${ew.customSqlSegment}")
    public void updateRedoCuoti(@Param(Constants.WRAPPER) QueryWrapper<CuoTiJi> wrapper);

    @Select("select * from cuotiji ${ew.customSqlSegment}")
    public List<CuoTiJi> queryCuotiForRedo(@Param(Constants.WRAPPER) QueryWrapper<CuoTiJi> wrapper);
    @Update("update cuotiji set userComment=#{userComment} where id=#{cuotiID}")
    public void UpdateUserComment(Integer cuotiID,String userComment);

    //新增一个学生的学习周记录
    @Insert("INSERT INTO cuotiji(category,problem,answer,sonProID,choiceNum,insertTime,field,esID,falseReasonID,aiComment,userComment,isRight," +
            "choice1,choice2,choice3,choice4,content,stuScore,score,stuID,stuAnswer) " +
            "VALUES (#{category},#{problem},#{answer},#{sonProID},#{choiceNum},#{insertTime},#{field},#{esID},#{falseReasonID},#{aiComment},#{userComment},#{isRight}," +
            "#{choice1},#{choice2},#{choice3},#{choice4},#{content},#{stuScore},#{score},#{stuID},#{stuAnswer})")
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id",
            keyColumn = "id", resultType = Integer.class, before = false)
    void insertCuotiji(CuoTiJi cuoTiJi);
    @Update("update cuotiji set redoNum=#{redoNum} where id=#{cuotiID}")
    void updateCuotiRedoNum(Integer redoNum,Integer cuotiID);
}
